package coml.jiyun.hp.reviewtopic.ui.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import coml.jiyun.hp.reviewtopic.DbUtil;
import coml.jiyun.hp.reviewtopic.R;
import coml.jiyun.hp.reviewtopic.base.BaseLazyFragment;
import coml.jiyun.hp.reviewtopic.bean.HomeBean;
import coml.jiyun.hp.reviewtopic.bean.HomeBeanTwo;
import coml.jiyun.hp.reviewtopic.presenter.HomePresenter;
import coml.jiyun.hp.reviewtopic.ui.activity.WebViewActivity;
import coml.jiyun.hp.reviewtopic.ui.adapter.RlvHomeAdapter;
import coml.jiyun.hp.reviewtopic.util.ToastUtil;
import coml.jiyun.hp.reviewtopic.view.HomeView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseLazyFragment<HomePresenter>implements HomeView {

    private RecyclerView mRlv;
    private RlvHomeAdapter rlvHomeAdapter;
    public int mPosition;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    private int page = 0;
    private ArrayList<HomeBean.DataBean.DatasBean> datasBeans1;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mPresenter.getHome(page);
    }

    @Override
    protected HomePresenter initPresenter() {
        return new HomePresenter() ;
    }

    public void initView(View inflate) {

        mRlv = inflate.findViewById(R.id.home_rlv);
        registerForContextMenu(mRlv);
        setHasOptionsMenu(true);

        mRlv.setLayoutManager(new LinearLayoutManager(getContext()));
        datasBeans1 = new ArrayList<>();
        rlvHomeAdapter = new RlvHomeAdapter(getActivity(),datasBeans1);
        mRlv.setAdapter(rlvHomeAdapter);

        rlvHomeAdapter.setOnItemLongClick(new RlvHomeAdapter.onItemLongClick() {
            @Override
            public void ca(View view, int i) {
                mPosition = i;
            }
        });

        rlvHomeAdapter.setOnItemClick(new RlvHomeAdapter.onItemClick() {
            @Override
            public void dj(int i) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                String link = rlvHomeAdapter.datasBeans.get(i).getLink();
                intent.putExtra("link",link);
                startActivity(intent);
            }
        });
        mSrl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                datasBeans1.clear();
                initData();
                rlvHomeAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void setHome(HomeBean homeBean) {
        rlvHomeAdapter.setDatasBeans(homeBean.getData().getDatas());

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(1,1,3,"网格布局");
        menu.add(1,2,3,"线性布局");
        menu.add(1,3,3,"瀑布流");
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                mRlv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                ToastUtil.showToastShort("网格布局");
                break;
            case 2:
                mRlv.setLayoutManager(new LinearLayoutManager(getActivity()));
                ToastUtil.showToastShort("线性布局");
                break;
            case 3:
                mRlv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
                ToastUtil.showToastShort("瀑布流");
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(4,5,6,"收藏");
        menu.add(4,6,6,"删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 5:
                collect();
                break;

            case 6:
                rlvHomeAdapter.shanchu(mPosition);
                ToastUtil.showToastShort("删除成功");
                break;
        }
        return super.onContextItemSelected(item);
    }

    private void collect() {
        List<HomeBean.DataBean.DatasBean> datasBeans = rlvHomeAdapter.datasBeans;
        HomeBeanTwo homeBeanTwo = new HomeBeanTwo();
        homeBeanTwo.setTitle(datasBeans.get(mPosition).getTitle());
        DbUtil.dbUtil().insert(homeBeanTwo);
        ToastUtil.showToastShort("收藏成功");
    }
}